package org.lm.base.interceptor;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;

import java.sql.Statement;
import java.util.Properties;

/**
 * @Auther: Min.Liu
 * @Description:
 * @Date: Created in 16:17 2025/4/9
 */
@Intercepts({
  @Signature(type = StatementHandler.class,method = "query",args = {Statement.class, ResultHandler.class})
})
public class sqlInterceptor implements Interceptor {
  Properties properties;
  Log log  = LogFactory.getLog(sqlInterceptor.class);

  @Override
  public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler target = (StatementHandler)invocation.getTarget();
    String sql = target.getBoundSql().getSql();
    sql= sql.replaceAll("\\n|\\r","");
    sql= sql.replaceAll(" +"," ");
    long start = System.currentTimeMillis();
    Object proceed;
    try {
       proceed = invocation.proceed();
    } finally {
      if (log.isDebugEnabled()) {
        log.debug("执行->["+sql+"]sql语句,耗时["+(System.currentTimeMillis()-start)+"毫秒]");
      }
    }
    return proceed;
  }

//  @Override
//  public Object plugin(Object target) {
////    return plugin(target);
//    return
//  }

  @Override
  public void setProperties(Properties properties) {
    this.properties = properties;
  }
}
